package com.blog.controller;

import com.blog.pojo.User;
import com.blog.service.UserService;
import com.blog.util.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;


@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    //跳转到登录页面
    @GetMapping({"/", "/admin"})
    public String loginPage() {
        return "login";
    }

    // 跳转到识别页面
    @GetMapping("/admin/catchFace")
    public String FaceLogin(HttpSession session, RedirectAttributes attributes) {
        System.out.println("敏感提示，即将前往拍照");
        return "catch";
    }

    // 人脸识别
    @PostMapping("/admin/faceCheck")
    @ResponseBody
    public ResponseEntity<Object> handleFaceCheck(@RequestParam("photo") MultipartFile photo) {
        try {
            // 假设这里调用了 userService.checkFace 方法进行人脸识别
            User user = userService.checkFace(photo);
            String jsonResponse = String.format("{\"success\": true, \"username\": \"%s\", \"password\": \"%s\"}", user.getUsername(), user.getPassword());
            return ResponseEntity.ok(jsonResponse);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("{\"success\": false, \"message\": \"人脸识生异常");
        }
    }

    //人脸登录
    @GetMapping("/admin/facelogin")
    public String facelogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            HttpSession session,
                            RedirectAttributes attributes) {
        User user = userService.FcheckUser(username, password);
        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user", user);
            return "main";
        } else {
            attributes.addFlashAttribute("message", "用户名和密码错误");
            return "redirect:/admin";
        }
    }

    //登录
    @PostMapping("/admin/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        HttpSession session,
                        RedirectAttributes attributes) {
        if (username.trim().isEmpty() && password.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名和密码不能为空");
            return "redirect:/admin";
        } else if (username.trim().isEmpty() || password.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名或密码不能为空");
            return "redirect:/admin";
        }

        User user = userService.checkUser(username, password);
        System.out.println(username);
        if (user != null) {
            user.setPassword(null);
            session.setAttribute("user", user);
            return "main";
        } else {
            attributes.addFlashAttribute("message", "用户名和密码错误");
            return "redirect:/admin";
        }
    }

    @PostMapping("/admin/register")
    public String registerUser(@RequestParam("username") String username,
                               @RequestParam("password") String password,
                               @RequestParam("confirmPassword") String confirmPassword,
                               HttpSession session,
                               RedirectAttributes attributes
    ) {
        // 对密码进行加密
        String encryptedPassword = MD5Utils.digest(password);

        // 判断用户名是否为空
        if (username.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "用户名不能为空");
            return "redirect:/admin";
        }


        // 判断密码是否为空
        if (encryptedPassword.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "密码不能为空");
            return "redirect:/admin";
        }

        // 判断确认密码是否为空
        if (confirmPassword.trim().isEmpty()) {
            attributes.addFlashAttribute("message", "确认密码不能为空");
            return "redirect:/admin";
        }

        // 判断用户名是否已存在
        if (userService.isUserExists(username)) {
            attributes.addFlashAttribute("message", "用户名已被注册");
            return "redirect:/admin";
        }



        // 判断两次密码是否一致
        if (!password.equals(confirmPassword)) {
            attributes.addFlashAttribute("message", "输入的两次密码不一致");
            return "redirect:/admin";
        }




        // 创建新的 User 对象
        userService.addUser(username, encryptedPassword);
        attributes.addFlashAttribute("message", "注册成功！请登录！");
        // 保存新用户到数据库

        return "redirect:/admin";
    }


    //注销
    @GetMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/admin";
    }


}
